﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using Microsoft.ApplicationBlocks.Data;

namespace AnyOffice.general.notify.show
{
    public partial class query : ModulePage
    {
        public string GetNew(string readers)
        {
            readers = "," + readers + ",";
            if (readers.IndexOf("," + new UserCache().UserId + ",") < 0)
            {
                return "<img src=/images/new.gif align=absmiddle>";
            }
            return string.Empty;
        }
        public string GetUserNames(string userIds)
        {
            return StringHelper.GetUserNames(base.Conn, userIds);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                DataTable table = new NotifyTypeDao().SelectAll(base.Conn);
                this.sltTypeId.DataSource = table;
                this.sltTypeId.DataBind();
                this.sltTypeId.Items.Insert(0, new ListItem("全部", ""));
                this.txtSendTimeMIN.Value = string.Concat(new object[] { DateTime.Now.Year, "-", DateTime.Now.Month.ToString(), "-1" });
                this.litNavTitle.Text = "公告通知查询";
            }
            if (this.Page.IsPostBack)
            {
                this.litNavTitle.Text = "公告通知查询结果";
                this.pnlMain.Visible = false;
                this.pnlLater.Visible = true;
                ArrayList list = new ArrayList();
                StringBuilder builder = new StringBuilder();
                builder.Append("SELECT ");
                builder.Append("\t\t[Notify].[NotifyId], ");
                builder.Append("\t\t[Notify].[Subject], ");
                builder.Append("\t\tdbo.GetDeptNames(CAST([Notify].[ToId] AS nvarchar(2000))) AS ToDeptNames, ");
                builder.Append("\t\tdbo.GetPrivNames(CAST([Notify].[ToPriv] AS nvarchar(2000))) AS ToPrivNames, ");
                builder.Append("\t\t[Notify].[ToUser], ");
                builder.Append("\t\t[Notify].[SendTime], ");
                builder.Append("\t\t[Notify].[Important], ");
                builder.Append("\t\t[Notify].[Readers], ");
                builder.Append("\t\t[Notify_Type].[TypeName], ");
                builder.Append("\t\t[Users].[UserName], ");
                builder.Append("\t\t[Department].[DeptName] ");
                builder.Append("\tFROM ");
                builder.Append("\t\t[dbo].[Notify], ");
                builder.Append("\t\t[dbo].[Notify_Type], ");
                builder.Append("\t\t[dbo].[Users], ");
                builder.Append("\t\t[dbo].[Department] ");
                builder.Append("\tWHERE ");
                builder.Append("\t\t[Notify].[TypeId] = [Notify_Type].[TypeId] ");
                if (this.txtSubject.Value != string.Empty)
                {
                    builder.Append(" AND [Notify].[Subject] LIKE '%' + @Subject + '%' ");
                    list.Add(new object[] { "@Subject", this.txtSubject.Value });
                }
                if (this.sltTypeId.Value != string.Empty)
                {
                    builder.Append(" AND [Notify].[TypeId] =  @TypeId ");
                    list.Add(new object[] { "@TypeId", Convert.ToInt32(this.sltTypeId.Value) });
                }
                if (this.UserIds.Value != string.Empty)
                {
                    builder.Append(" AND CHARINDEX(',' + [Notify].[FromId] + ',',',' + CAST(@UserIds AS nvarchar(2000)) + ',') > 0 ");
                    list.Add(new object[] { "@UserIds", this.UserIds.Value });
                }
                if (this.txtSendTimeMIN.Value != string.Empty)
                {
                    builder.Append(" AND [Notify].[SendTime] >= @MinDate ");
                    list.Add(new object[] { "@MinDate", Convert.ToDateTime(this.txtSendTimeMIN.Value + " 00:00:00") });
                }
                if (this.txtSendTimeMAX.Value != string.Empty)
                {
                    builder.Append(" AND [Notify].[SendTime] <= @MaxDate ");
                    list.Add(new object[] { "@MaxDate", Convert.ToDateTime(this.txtSendTimeMAX.Value + " 23:59:59") });
                }
                if (this.txtContent.Value != string.Empty)
                {
                    builder.Append(" AND [Notify].[Content] LIKE '%' + @Content + '%' ");
                    list.Add(new object[] { "@Content", this.txtContent.Value });
                }
                builder.Append("\t\tAND [Notify].[FromId] = [Users].[UserId] ");
                builder.Append("\t\tAND [Users].[DeptId] = [Department].[DeptId] ");
                builder.Append("\t\tAND ( ");
                builder.Append("\t\t\tCAST([ToId] AS nvarchar(20)) = 'ALL_DEPT' ");
                builder.Append("\t\t\tOR (CHARINDEX(',' + CAST(@DeptId AS nvarchar(20)) + ',',',' + CAST([ToId] AS nvarchar(2000)) + ',') > 0) ");
                builder.Append("\t\t\tOR (CHARINDEX(',' + CAST(@PrivId AS nvarchar(20)) + ',',',' + CAST([ToPriv] AS nvarchar(2000)) + ',') > 0) ");
                builder.Append("\t\t\tOR (CHARINDEX(',' + @UserId + ',',',' + CAST([ToUser] AS nvarchar(4000)) + ',') > 0) ");
                builder.Append("\t\t    ) ");
                builder.Append("\t\tAND (([BeginDate] IS NULL) OR ([BeginDate] <= (CAST(YEAR(GETDATE()) AS nvarchar) + '-' + CAST(MONTH(GETDATE()) AS nvarchar) + '-' + CAST(DAY(GETDATE()) AS nvarchar)))) ");
                builder.Append("\t\tAND (([EndDate] IS NULL) OR ([EndDate] >= (CAST(YEAR(GETDATE()) AS nvarchar) + '-' + CAST(MONTH(GETDATE()) AS nvarchar) + '-' + CAST(DAY(GETDATE()) AS nvarchar)))) ");
                builder.Append("\tORDER BY ");
                builder.Append("\t\t[Notify].[Important] DESC, ");
                builder.Append("\t\t[Notify].[NotifyId] DESC ");
                UserCache cache = new UserCache();
                list.Add(new object[] { "@DeptId", cache.DeptId });
                list.Add(new object[] { "@PrivId", cache.PrivId });
                list.Add(new object[] { "@UserId", base.User.Identity.Name });
                SqlParameter[] commandParameters = new SqlParameter[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    object[] objArray11 = (object[])list[i];
                    commandParameters[i] = new SqlParameter(objArray11[0].ToString(), objArray11[1]);
                }
                DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , builder.ToString(), commandParameters).Tables[0];
                if (table2.Rows.Count > 0)
                {
                    this.rptNotify.DataSource = table2;
                    this.rptNotify.DataBind();
                }
                else
                {
                    this.litMessage.Text = base.ShowMessage("无符合条件的公告通知");
                }
            }
        }

    }
}
